/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.NVK;

import DAO.KetNoi;
import DAO.LoaiSanPhamDao;
import POJO.LoaiSanPham;
import java.util.ArrayList;
import java.sql.Statement;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Huê
 */
public class frQuanLyLoaiSanPham extends javax.swing.JInternalFrame{

	/**
	 * Creates new form GuiNhapKho
	 */
	public frQuanLyLoaiSanPham() {
		initComponents();
		LoadLoaiSanPham();
	}
	DefaultTableModel model = null;// cai bien nay phai de toan cu de co the su dung ve sau

	public void LoadLoaiSanPham() {
		int stt = 1;
		ArrayList<LoaiSanPham> dsLoaiSanPham = LoaiSanPhamDao.layDanhSach();
		String[] columns = new String[]{"STT", "Mã loại", "Loại sản phẩm"};
		DefaultTableModel model = new DefaultTableModel(null, columns);
		for (LoaiSanPham lsp : dsLoaiSanPham) {
			Object[] items = new Object[]{stt,
				lsp.getMaLSP(),
				lsp.getTenLSP()
			};
			model.addRow(items);
			stt++;
		}
		jtDSLoaiSP.setModel(model);
		int[] va = {5, 15, 30};// ti le cac cot
		int[] pt = new int[3];//bien du  do rong cac cot tinh theo ti le
		for (int l = 0; l < 3; l++) {// duyet qua cac do rong
			pt[l] = jtDSLoaiSP.getWidth() * va[l] / 100;//do rong = bang be rong cua table * tiLe/ chia lai 100
			jtDSLoaiSP.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);// set do rong cho cac cot

		}
		// lay dong dau tien cho vao texbox
		if (model.getRowCount() > 0)// kiem tra de khong bi loi khi modle rong
		{
			setTextBox(0);
		}

	}

	public void LoadMa() {
		LoaiSanPhamDao lsp = new LoaiSanPhamDao();
		int ma = lsp.layMaMax() + 1;
		jtftMaLSP.setText(Integer.toString(ma));
	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jPanel1 = new javax.swing.JPanel();
              jtftMaLSP = new javax.swing.JTextField();
              jtftTenLSP = new javax.swing.JTextField();
              jLabel1 = new javax.swing.JLabel();
              jLabel2 = new javax.swing.JLabel();
              jPanel2 = new javax.swing.JPanel();
              jtbnMoi = new javax.swing.JButton();
              jbtnThem = new javax.swing.JButton();
              jbtnXoa = new javax.swing.JButton();
              jbtnSua = new javax.swing.JButton();
              jbtnHienThi = new javax.swing.JButton();
              jPanel3 = new javax.swing.JPanel();
              jScrollPane1 = new javax.swing.JScrollPane();
              jtDSLoaiSP = new javax.swing.JTable();
              jPanel4 = new javax.swing.JPanel();
              jLabel5 = new javax.swing.JLabel();

              setClosable(true);
              setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
              setIconifiable(true);
              setMaximizable(true);
              setResizable(true);

              jtftMaLSP.setEditable(false);
              jtftMaLSP.addKeyListener(new java.awt.event.KeyAdapter() {
                     public void keyReleased(java.awt.event.KeyEvent evt) {
                            jtftMaLSPKeyReleased(evt);
                     }
              });

              jtftTenLSP.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jtftTenLSPActionPerformed(evt);
                     }
              });
              jtftTenLSP.addInputMethodListener(new java.awt.event.InputMethodListener() {
                     public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                     }
                     public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                            jtftTenLSPInputMethodTextChanged(evt);
                     }
              });
              jtftTenLSP.addKeyListener(new java.awt.event.KeyAdapter() {
                     public void keyReleased(java.awt.event.KeyEvent evt) {
                            jtftTenLSPKeyReleased(evt);
                     }
              });
              jtftTenLSP.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
                     public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                            jtftTenLSPVetoableChange(evt);
                     }
              });

              jLabel1.setText("Mã loại sản phẩm:");

              jLabel2.setText("Tên loại sản phẩm:");

              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(124, 124, 124)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                   .addComponent(jtftTenLSP, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                   .addComponent(jtftMaLSP))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel1Layout.setVerticalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jtftMaLSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel1))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jtftTenLSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel2))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              jtbnMoi.setText("Mới");
              jtbnMoi.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jtbnMoiActionPerformed(evt);
                     }
              });

              jbtnThem.setText("Thêm");
              jbtnThem.setEnabled(false);
              jbtnThem.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnThemActionPerformed(evt);
                     }
              });

              jbtnXoa.setText("Xóa");
              jbtnXoa.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnXoaActionPerformed(evt);
                     }
              });

              jbtnSua.setText("Sửa");
              jbtnSua.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnSuaActionPerformed(evt);
                     }
              });

              jbtnHienThi.setText("Hiển thị");
              jbtnHienThi.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jbtnHienThiActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
              jPanel2.setLayout(jPanel2Layout);
              jPanel2Layout.setHorizontalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(175, 175, 175)
                            .addComponent(jtbnMoi)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jbtnThem)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jbtnXoa)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jbtnSua)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jbtnHienThi)
                            .addContainerGap(172, Short.MAX_VALUE))
              );
              jPanel2Layout.setVerticalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jtbnMoi)
                                   .addComponent(jbtnThem)
                                   .addComponent(jbtnXoa)
                                   .addComponent(jbtnSua)
                                   .addComponent(jbtnHienThi))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách loại sản phẩm"));

              jtDSLoaiSP.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Mã loại SP", "Tên loại sản phẩm"
                     }
              ));
              jtDSLoaiSP.addMouseListener(new java.awt.event.MouseAdapter() {
                     public void mouseClicked(java.awt.event.MouseEvent evt) {
                            jtDSLoaiSPMouseClicked(evt);
                     }
              });
              jScrollPane1.setViewportView(jtDSLoaiSP);

              javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
              jPanel3.setLayout(jPanel3Layout);
              jPanel3Layout.setHorizontalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
              );
              jPanel3Layout.setVerticalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
              );

              jLabel5.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
              jLabel5.setText("QUẢN LÝ LOẠI SẢN PHẨM");

              javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
              jPanel4.setLayout(jPanel4Layout);
              jPanel4Layout.setHorizontalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel4Layout.setVerticalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel5)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(0, 0, 0))
              );
              layout.setVerticalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(0, 0, 0))
              );

              pack();
       }// </editor-fold>//GEN-END:initComponents

	private boolean KiemTra() {
		if (jtftTenLSP.getText().trim().equals("") == true) {
			return true;
		}
		return false;
	}
       private void jbtnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSuaActionPerformed
		// TODO add your handling code here:
		int maLSP = Integer.parseInt(this.jtftMaLSP.getText());
		String tenLSP = this.jtftTenLSP.getText();
		LoaiSanPham lsp = new LoaiSanPham();
		lsp.setMaLSP(maLSP);
		lsp.setTenLSP(tenLSP);

		boolean kq = LoaiSanPhamDao.capNhat(lsp);
		if (kq == true) {
			JOptionPane.showMessageDialog(this, "Cập nhật loại sản phẩm thành công", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
			LoadLoaiSanPham();
		} else {
			JOptionPane.showMessageDialog(this, "Cập nhật loại sản phẩm thất bại", "Cảnh báo!!!", JOptionPane.WARNING_MESSAGE);
		}


       }//GEN-LAST:event_jbtnSuaActionPerformed

       private void jtbnMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtbnMoiActionPerformed
		// TODO add your handling code here:
		jbtnThem.setEnabled(true);
		jbtnSua.setEnabled(false);
		jbtnXoa.setEnabled(false);
		jtbnMoi.setEnabled(false);
		LoadMa();
		jtftTenLSP.setText(null);
		jtftTenLSP.requestFocusInWindow();

       }//GEN-LAST:event_jtbnMoiActionPerformed

	private void vbtMoi() {
		jtftMaLSP.setEnabled(false);
		jtbnMoi.setEnabled(false);
		jbtnThem.setEnabled(true);
		jbtnXoa.setEnabled(false);
		jbtnSua.setEnabled(false);
		jbtnHienThi.setEnabled(false);
	}

	private void vbtBinhThuong() {
		jtftMaLSP.setEnabled(true);
		jtbnMoi.setEnabled(true);
		jbtnThem.setEnabled(false);
		jbtnXoa.setEnabled(true);
		jbtnSua.setEnabled(true);
		jbtnHienThi.setEnabled(true);
	}

	private void vbtSua() {
		jtftMaLSP.setEnabled(false);
		jtbnMoi.setEnabled(true);
		jbtnThem.setEnabled(false);
		jbtnXoa.setEnabled(true);
		jbtnSua.setEnabled(true);
		jbtnHienThi.setEnabled(true);
	}
       private void jbtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThemActionPerformed
		// TODO add your handling code here:
		if (KiemTra() == true) {
			JOptionPane.showMessageDialog(this, "Nhập thiếu thông tin nhập vào cho đủ");
			jtftTenLSP.requestFocusInWindow();
			return;
		}
		int maLSP = Integer.parseInt(this.jtftMaLSP.getText());
		String tenLSP = this.jtftTenLSP.getText();
		LoaiSanPham lsp = new LoaiSanPham();
		lsp.setMaLSP(maLSP);
		lsp.setTenLSP(tenLSP);
		boolean kq = LoaiSanPhamDao.them(lsp);
		if (kq == true) {
			JOptionPane.showMessageDialog(this, "Thêm loai sản phẩm thành công", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
			LoadLoaiSanPham();
			HienThi();
		} else {
			JOptionPane.showMessageDialog(this, "Thêm loai sản phẩm thất bại", "Cảnh báo!!!", JOptionPane.WARNING_MESSAGE);
		}
       }//GEN-LAST:event_jbtnThemActionPerformed

       private void jtftTenLSPInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jtftTenLSPInputMethodTextChanged
		// TODO add your handling code here:
       }//GEN-LAST:event_jtftTenLSPInputMethodTextChanged

       private void jtftTenLSPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtftTenLSPActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_jtftTenLSPActionPerformed

       private void jtftTenLSPVetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_jtftTenLSPVetoableChange
		// TODO add your handling code here:
       }//GEN-LAST:event_jtftTenLSPVetoableChange

       private void jtftTenLSPKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtftTenLSPKeyReleased
		// TODO add your handling code here:
		jtftTenLSP.setText(jtftTenLSP.getText().replaceAll("[!';:]", ""));
       }//GEN-LAST:event_jtftTenLSPKeyReleased

    private void jbtnHienThiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnHienThiActionPerformed
	    // TODO add your handling code here:
	    HienThi();
	    LoadLoaiSanPham();
    }//GEN-LAST:event_jbtnHienThiActionPerformed

	private void HienThi() {
		jtbnMoi.setEnabled(true);
		jbtnThem.setEnabled(false);
		jbtnXoa.setEnabled(true);
		jbtnSua.setEnabled(true);
	}

	private void setTextBox(int dong) {// viet ham de tai su dung
		String ma = this.jtDSLoaiSP.getValueAt(dong, 1).toString();
		String ten = this.jtDSLoaiSP.getValueAt(dong, 2).toString();
		this.jtftMaLSP.setText(ma);
		this.jtftTenLSP.setText(ten);
	}

    private void jtDSLoaiSPMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtDSLoaiSPMouseClicked
	    // TODO add your handling code here:
	    int dong = this.jtDSLoaiSP.getSelectedRow();

	    if (dong < jtDSLoaiSP.getRowCount() && dong > -1) {
		    setTextBox(dong);
	    }
	    HienThi();
    }//GEN-LAST:event_jtDSLoaiSPMouseClicked

    private void jbtnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnXoaActionPerformed
	    // TODO add your handling code here:
	    int maLSP = Integer.parseInt(jtftMaLSP.getText());

	    boolean kq = LoaiSanPhamDao.xoa(maLSP);
	    if (kq == true) {
		    JOptionPane.showMessageDialog(this, "Xoá loai sản phẩm thành công ", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
		    LoadLoaiSanPham();
	    } else {
		    JOptionPane.showMessageDialog(this, "Xoá loai sản phẩm thất bại", "Cảnh báo!!!", JOptionPane.WARNING_MESSAGE);
	    }

    }//GEN-LAST:event_jbtnXoaActionPerformed

       private void jtftMaLSPKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtftMaLSPKeyReleased
		// TODO add your handling code here:
		jtftMaLSP.setText(jtftMaLSP.getText().replaceAll("[!';:]", ""));

       }//GEN-LAST:event_jtftMaLSPKeyReleased

	
       // Variables declaration - do not modify//GEN-BEGIN:variables
       private javax.swing.JLabel jLabel1;
       private javax.swing.JLabel jLabel2;
       private javax.swing.JLabel jLabel5;
       private javax.swing.JPanel jPanel1;
       private javax.swing.JPanel jPanel2;
       private javax.swing.JPanel jPanel3;
       private javax.swing.JPanel jPanel4;
       private javax.swing.JScrollPane jScrollPane1;
       private javax.swing.JButton jbtnHienThi;
       private javax.swing.JButton jbtnSua;
       private javax.swing.JButton jbtnThem;
       private javax.swing.JButton jbtnXoa;
       private javax.swing.JTable jtDSLoaiSP;
       private javax.swing.JButton jtbnMoi;
       private javax.swing.JTextField jtftMaLSP;
       private javax.swing.JTextField jtftTenLSP;
       // End of variables declaration//GEN-END:variables
}
